package com.shire.shire_interface.service.account.impl;

import com.shire.shire_interface.dao.accounts.DeviceKeyDao;
import com.shire.shire_interface.model.account.DeviceKey;
import com.shire.shire_interface.model.account.DeviceKeyExample;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@CacheConfig(cacheNames = {"device_key"})
@Service
public class DeviceKeyService {


    @Resource
    private DeviceKeyDao deviceKeyDao;

    @Cacheable(key = "#key")
    public DeviceKey selectByKey(String key){
        DeviceKeyExample example = new DeviceKeyExample();
        example.createCriteria().andDevicekeyEqualTo(key);
        List<DeviceKey> keys = deviceKeyDao.selectByExample(example);
        if (keys.size()>0)
            return keys.get(0);
        return null;
    }
    @CacheEvict(key = "#targetClass + #methodName +#p0.devicekey")
    public int updateDeviceKey(DeviceKey key){
        return deviceKeyDao.updateByPrimaryKeySelective(key);
    }
}
